home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / dev / e / yaec.lha / examples / ecode.e < prev    next >
Text File  |  2001-08-12  |  7KB  |  315 lines

  1. LINKOBJECT
  2.  
  3. MODULE 'exec/memory',
  4.        'hardware/custom',
  5.        'utility/hooks'
  6.  
  7.        -> ecode.e - from Jasons R Hulance`s UsefulV2.
  8.  
  9.        -> YAEC Notes :
  10.        -> - Compiled as external linklib.
  11.        -> - Function-names changed to start with upper-case.
  12.        -> - Alot changes in asm-parts, but still recogniseable.
  13.        -> YAEC1.6a :
  14.        -> - Added ECodeUtilHook().   Anything else ?
  15.        -> - Fixed some various bugs.
  16.  
  17.  
  18.        RAISE "MEM" IF New()=NIL
  19.  
  20. ->EXPORT PROC Test(x,y,z) IS WriteF('testing \d,\d,\d\n',x,y,z)
  21.   
  22. -> Wraps an E function so it can still access globals, even from other tasks.
  23. EXPORT PROC ECode(func) IS setup(func,({start}),({end})-({start}))
  24.  
  25. -> Wraps an E function as above, but also preserves the non-scratch registers.
  26. EXPORT PROC ECodePreserve(func) IS setup(func,({pStart}),({pEnd})-({pStart}))
  27.  
  28. -> Wraps an E function for use with createTask()
  29. EXPORT PROC ECodeTask(func) IS ECode(func)
  30.  
  31. -> Wraps an E function for use as an ASL hook
  32. EXPORT PROC ECodeASLHook(func) IS setup(func,({aStart}),({aEnd})-({aStart}))
  33.  
  34. -> Wraps an E function for use as an CX custom function
  35. EXPORT PROC ECodeCxCustom(func) IS setup(func,({cStart}),({cEnd})-({cStart}))
  36.  
  37. -> Wraps an E function for use as a GEL collision function
  38. EXPORT PROC ECodeCollision(func) IS ECodeCxCustom(func)
  39.  
  40. -> Wraps an E function for use as an interrupt handler
  41. EXPORT PROC ECodeIntHandler(func) IS setup(func,({hStart}),({hEnd})-({hStart}))
  42.  
  43. -> Wraps an E function for use as an interrupt server
  44. EXPORT PROC ECodeIntServer(func) IS setup(func,({sStart}),({sEnd})-({sStart}))
  45.  
  46. -> Wraps an E function for use as a software interrupt
  47. EXPORT PROC ECodeSoftInt(func) IS setup(func,({iStart}),({iEnd})-({iStart}))
  48.  
  49. -> Wraps an E function as eCode(), but swaps the order of two args
  50. EXPORT PROC ECodeSwapArgs(func) IS setup(func,({oStart}),({oEnd})-({oStart}))
  51.  
  52. EXPORT PROC ECodeDispose(mem:PTR TO LONG)
  53.    IF mem
  54.       IF mem::hook.entry = {entry} THEN RETURN FastDispose(mem, SIZEOF ecodehook)
  55.       mem := mem - 12
  56.       IF mem[2] THEN Dispose(mem[2])
  57.       Dispose(mem)
  58.    ENDIF
  59. ENDPROC
  60.  
  61. PROC setup(func, addr, len) HANDLE
  62.   DEF mem:PTR TO LONG, a4
  63.   mem := New(len) ->mem:=NewM(len, MEMF_PUBLIC)
  64.   -> Fully relocatable code can be copied to another memory location
  65.   CopyMem(addr, mem, len)
  66.   mem[]++:=func
  67.   ->a4 := A4 ->MOVE.L A4, a4
  68.   mem[]++:= A4 -> I just know this is okey.
  69.   mem[]++:= New(400)
  70.   IF KickVersion(36) THEN CacheClearU()  -> Write out the cache (68040 especially bad...)
  71.   RETURN mem
  72. EXCEPT
  73.   RETURN NIL
  74. ENDPROC
  75.  
  76. OBJECT ecodehook OF hook
  77.    priv[100]:ARRAY OF LONG
  78. ENDOBJECT
  79.  
  80. -> Wraps an E function for use as an Utility hook
  81. EXPORT PROC ECodeUtilHook(func, data)
  82.    DEF h:PTR TO ecodehook
  83.    NEW h
  84.    h.subentry := func
  85.    h.entry := {entry}
  86.    h.data := data
  87. ENDPROC h 
  88.  
  89. ->EXPORT PROC InstallHook(hook, func)
  90. ->ENDPROC
  91.  
  92. entry:
  93. ASM ' movem.l d2-d7/a2-a6,-(a7)'  -> save regs
  94. ASM ' move.l  a0,-(a7)'           -> hook
  95. ASM ' move.l  a2,-(a7)'           -> obj
  96. ASM ' move.l  a1,-(a7)'           -> msg
  97. ASM ' move.l  a4storage, a4'      -> reinstate a4
  98. ASM ' lea.l   20(a0), a2'         -> a2space
  99. ASM ' moveq.l #-1, d7'            -> clear exception
  100. ASM ' move.l  12(a0),a0'          -> get sub-entry
  101. ASM ' jsr     (a0)'               -> execute function
  102. ASM ' movem.l (a7)+,d2-d7/a2-a6'  -> restore regs
  103. ASM ' rts'                        -> return
  104.  
  105. start:
  106. ASM
  107. .function:
  108.   dc.l 0
  109. .storea4:
  110.   dc.l 0
  111. .a2space:
  112.   dc.l 0
  113. .entry:
  114.   move.l .storea4(pc), a4      ;-> restore a4
  115.   move.l .a2space(pc), a2
  116.   move.l .function(pc), a6
  117.   moveq.l #-1, d7
  118.   jsr (a6)                         ;-> Call real function
  119.   rts
  120. ENDASM                       
  121. end:
  122.  
  123.  
  124.  
  125. pStart:
  126. ASM
  127. .pfunction:
  128.   dc.l 0
  129. .pstorea4:
  130.   dc.l 0
  131. .a2space:
  132.   dc.l 0
  133. .pentry:
  134.   movem.l d2-d7/a2-a6, -(a7)  ;-> preserve registers
  135.   move.l .pstorea4(pc), a4    ; -> restore a4
  136.   move.l .a2space(pc), a2
  137.   move.l .pfunction(pc), a5
  138.   moveq.l #-1, d7
  139.   jsr (a5)                    ;-> call real function
  140.   movem.l (a7)+, d2-d7/a2-a6  ;-> restore registers
  141.   rts
  142. ENDASM
  143. pEnd:
  144.  
  145. -> aslhook
  146. aStart:
  147. ASM
  148. .afunction:
  149.   dc.l 0
  150. .astorea4:
  151.   dc.l 0
  152. .a2space:
  153.   dc.l 0
  154. .aentry:
  155.   lea .astack(pc), a0
  156.   movem.l d2-d7/a1-a6, (a0)   ;-> preserve registers
  157.   move.l .astorea4(pc), a4    ; -> restore a4
  158.   move.l .afunction(pc), a6
  159.   move.l .a2space(pc), a2
  160.  
  161.   move.l 4(a7), a0            ; swap 3 args
  162.   move.l 8(a7), a1
  163.   move.l 12(a7), d0
  164.   move.l a0, -(a7)
  165.   move.l a1, -(a7)
  166.   move.l d0, -(a7)
  167.   moveq.l #-1, d7
  168.   jsr (a6)                    ;-> call real function
  169.   lea .astack(pc), a0
  170.   movem.l (a0), d2-d7/a1-a6   ;-> restore registers
  171.   rts
  172. .astack:
  173.   dc.l 2,3,4,5,6,7,1,2,3,4,5,6,0
  174. ENDASM
  175. aEnd:
  176.  
  177. ->cx/collision
  178. cStart:
  179. ASM
  180. .cfunction:
  181.   dc.l 0
  182. .cstorea4:
  183.   dc.l 0
  184. .a2space:
  185.   dc.l 0
  186. .centry:
  187.   lea .cstack(pc), a0
  188.   movem.l d2-d7/a1-a6, (a0)   ;-> preserve registers
  189.  
  190.   move.l .cstorea4(pc), a4    ; -> restore a4
  191.   move.l .cfunction(pc), a6
  192.   move.l .a2space(pc), a2
  193.  
  194.   move.l 4(a7), a0            ; swap 2 args
  195.   move.l 8(a7), a1
  196.   move.l a0, -(a7)
  197.   move.l a1, -(a7)
  198.   moveq.l #-1, d7
  199.   jsr (a6)                    ;-> call real function
  200.   lea .cstack(pc), a0
  201.   movem.l (a0), d2-d7/a1-a6   ;-> restore registers
  202.   rts
  203. .cstack:
  204.   dc.l 2,3,4,5,6,7,1,2,3,4,5,6,0
  205. ENDASM
  206. cEnd:
  207.  
  208.  
  209. hStart:
  210. ASM
  211. .hfunction:
  212.   dc.l 0
  213. .hstorea4:
  214.   dc.l 0
  215. .a2space:
  216.   dc.l 0
  217. .hentry:
  218.   lea .hstack(pc), a0
  219.   movem.l d2-d7/a2-a4, (a0)  ;-> preserve registers
  220.   move.l .hstorea4(pc), a4   ; -> restore a4
  221.   move.l .hfunction(pc), a0
  222.   movem.l d1/a1, -(a7)       ;-> push d1 and a1 as arguments
  223.   move.l .a2space(pc), a2
  224.   moveq.l #-1, d7
  225.   jsr (a0)                   ;-> call real function
  226.   lea .hstack(pc), a0
  227.   movem.l (a0), d2-d7/a2-a4  ;-> restore registers
  228.   rts
  229. .hstack:
  230.   dc.l 2,3,4,5,6,7,2,3,4,0
  231. ENDASM
  232. hEnd:
  233.  
  234.  
  235. sStart:
  236. ASM
  237. .sfunction:
  238.  dc.l 0
  239. .sstorea4:
  240.  dc.l 0
  241. .a2space:
  242.  dc.l 0
  243. .sentry:
  244.  lea .sstack(pc), a0
  245.  movem.l d2-d7/a2-a4, (a0) ;  -> preserve registers
  246.  move.l .sstorea4(pc), a4  ;   -> restore a4
  247.  move.l .sfunction(pc), a6
  248.  move.l a1, -(a7)         ;   -> push a1 as an argument
  249.  move.l .a2space(pc), a2
  250.  moveq.l #-1, d7
  251.  jsr (a6)                ;    -> call real function
  252.  lea .sstack(pc), a0
  253.  movem.l (a0), d2-d7/a2-a4 ;   -> restore registers
  254.  move.l #$DFF000, a0  ;-> CUSTOMADDR
  255.  tst.l d0 ;                    -> set z flag according to func result
  256.  rts
  257. .sstack:
  258.  dc.l 2,3,4,5,6,7,2,3,4,0
  259. ENDASM
  260. sEnd:
  261.  
  262.  
  263. iStart:
  264. ASM
  265. .ifunction:
  266.   dc.l 0
  267. .istorea4:
  268.   dc.l 0
  269. .a2space:
  270.   dc.l 0
  271. .ientry:
  272.   lea .istack(pc), a0
  273.   movem.l d2-d7/a2-a4/a6, (a0) ; -> preserve registers
  274.   move.l .istorea4(pc), a4     ;  -> restore a4
  275.   move.l .ifunction(pc), a6
  276.   move.l a1, -(a7)             ; -> push a1 as an argument
  277.   move.l .a2space(pc), a2
  278.   moveq.l #-1, d7
  279.   jsr (a6)                     ; -> call real function
  280.   lea .istack(pc), a0
  281.   movem.l (a0), d2-d7/a2-a4/a6 ; -> restore registers
  282.   rts
  283. .istack:
  284.   dc.l 2,3,4,5,6,7,2,3,4,6,0
  285. ENDASM
  286. iEnd:
  287.  
  288.  
  289. oStart:
  290. ASM
  291. .ofunction:
  292.   dc.l 0
  293. .ostorea4:
  294.   dc.l 0
  295. .a2space:
  296.   dc.l 0
  297. .oentry:
  298.   move.l 4(a7), a0             ;-> swap 2 arguments
  299.   move.l 8(a7), a1
  300.   move.l a2, -(a7)            ; save a2
  301.   move.l a0, -(a7)
  302.   move.l a1, -(a7)
  303.   move.l .ostorea4(pc), a4     ; -> restore a4
  304.   move.l .ofunction(pc), a0
  305.   move.l .a2space(pc), a2
  306.   moveq.l #-1, d7
  307.   jsr (a0)                          ;-> Call real function
  308.   move.l (a7)+, a2              ; restore a2
  309.   rts
  310. ENDASM                       
  311. oEnd:
  312.  
  313.  
  314.  
  315.